<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
            <el-alert
                type="warning"
                title="1.查看优惠券领取记录；2.未使用的优惠券可以作废。"
                :closable="false"
                show-icon
            />
            <el-form ref="formRef" class="mb-[-16px] mt-[15px]" :model="queryParams" :inline="true">
                <el-form-item label="用户信息">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.user_info"
                        placeholder="请输入用户编号/昵称"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="优惠券名称">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.name"
                        placeholder="请输入"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>

                <el-form-item label="领取方式">
                    <el-select v-model="queryParams.get_type" placeholder="全部">
                        <el-option label="全部" value="" />
                        <el-option label="用户领取" value="1" />
                        <el-option label="系统赠送" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="发放人">
                    <el-select v-model="queryParams.operator_id" placeholder="全部">
                        <el-option label="全部" value="" />
                        <el-option
                            v-for="item in optionsData.lists.lists"
                            :value="item.id"
                            :key="item.id"
                            :label="item.name"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="领取时间">
                    <daterange-picker
                        class="w-[385px]"
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <!-- 导航栏 -->
            <el-tabs v-model="queryParams.status" class="demo-tabs" @tab-click="handleTabclick">
                <el-tab-pane :label="`全部(${pager.extend.all || '0'})`" :name="''" />
                <el-tab-pane :label="`未使用(${pager.extend.not || '0'})`" :name="0" />
                <el-tab-pane :label="`已使用(${pager.extend.use || '0'})`" :name="1" />
                <el-tab-pane :label="`已过期(${pager.extend.overdue || '0'})`" :name="2" />
                <el-tab-pane :label="`已作废(${pager.extend.void || '0'})`" :name="3" />

                <el-button
                    @click="deleteall"
                    class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"
                    :disabled="ids.length > 0 ? false : true"
                    >批量作废</el-button
                >

                <!-- 表格 -->
                <el-table
                    size="large"
                    :data="pager.lists"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="60" />
                    <el-table-column label="用户昵称" width="360">
                        <template #default="{ row }">
                            <div class="flex items-center">
                                <div>
                                    <el-avatar
                                        :src="row.avatar"
                                        style="height: 60px; min-width: 60px"
                                    />
                                </div>

                                <div class="ml-[10px] text_hidden">{{ row.nickname }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="优惠券名称" prop="name" />
                    <el-table-column label="领取方式">
                        <template #default="{ row }">
                            <span
                                :class="{
                                    'text-tip': row.get_type_text === '系统赠送' ? true : false
                                }"
                            >
                                {{ row.get_type_text }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="发放人" prop="operator_text" />
                    <el-table-column label="领取时间" prop="create_time" />
                    <el-table-column label="过期时间" prop="invalid_time" />
                    <el-table-column label="使用状态">
                        <template #default="{ row }">
                            <el-tag
                                class="ml-2"
                                type="success"
                                v-if="row.status_text === '已使用'"
                                >{{ row.status_text }}</el-tag
                            >
                            <el-tag
                                class="ml-2"
                                type="danger"
                                v-if="row.status_text === '未使用'"
                                >{{ row.status_text }}</el-tag
                            >
                            <el-tag class="ml-2" type="info" v-if="row.status_text === '已过期'">{{
                                row.status_text
                            }}</el-tag>

                            <el-tag class="ml-2" type="info" v-if="row.status_text === '已作废'">{{
                                row.status_text
                            }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="使用时间" prop="use_time" />
                    <el-table-column label="操作" min-width="160" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-if="row.status_text == '未使用'"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                作废
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { getcouponlists, setcouponlists } from '@/api/application'
import { adminLists } from '@/api/perms/admin'
import { useDictOptions } from '@/hooks/useDictOptions'
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'

const queryParams = reactive({
    user_info: '',
    name: '',
    get_type: '',
    operator_id: '',
    start_time: '',
    end_time: '',
    status: ''
})

//分页处理
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getcouponlists,
    params: queryParams
})
getLists()

const { optionsData } = useDictOptions<{
    lists: any[]
}>({
    lists: {
        api: adminLists
    }
})

//删除商品
const ids = ref<any[]>([])
const handleSelectionChange = (val: any[]) => {
    ids.value = val.map((item) => {
        return item.id
    })
}
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要作废？')
    await setcouponlists({ id: [id] })
    getLists()
}
//批量删除
const deleteall = async () => {
    await feedback.confirm('确定要作废？')
    await setcouponlists({ id: ids.value })
    getLists()
}

//切换导航栏
const handleTabclick = async () => {
    await nextTick()
    getLists()
}
</script>

<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}

.ls-input {
    width: 280px;
}
.text-tip {
    color: #67c23a;
}
</style>
